{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# API Use Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "requests Version:2.21.0 \n",
      "CWD:D:\\Desktop\\ServerAPI_Demo \n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import requests\n",
    "\n",
    "print('requests Version:{} '.format(requests.__version__))\n",
    "print('CWD:{} '.format(os.getcwd()))  # 当前路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "URL = 'http://192.168.192.234:8888'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "URL         : http://192.168.192.234:8888/health\n",
      "Path_URL    : /health\n",
      "StatusCode  : 200\n",
      "Method      : POST\n",
      "Encoding    : utf-8\n",
      "Headers     : {'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '85', 'Server': 'Werkzeug/0.14.1 Python/3.7.1', 'Date': 'Mon, 11 Jan 2021 04:17:09 GMT'}\n",
      "Body        : None\n",
      "Cookies     : <RequestsCookieJar[]>\n",
      "Content     : b'Start: 20210111 12:16:29 ........... Status: <OK> ........... Time: 20210111 12:17:09'\n",
      "Text        : Start: 20210111 12:16:29 ........... Status: <OK> ........... Time: 20210111 12:17:09\n"
     ]
    }
   ],
   "source": [
    "# API 状态监测\n",
    "api_response = requests.post('{}/health'.format(URL))\n",
    "print('{:12s}: {}'.format('URL', api_response.url))\n",
    "print('{:12s}: {}'.format('Path_URL', api_response.request.path_url))\n",
    "print('{:12s}: {}'.format('StatusCode', api_response.status_code))\n",
    "print('{:12s}: {}'.format('Method', api_response.request.method))\n",
    "print('{:12s}: {}'.format('Encoding', api_response.encoding))\n",
    "print('{:12s}: {}'.format('Headers', api_response.headers))\n",
    "print('{:12s}: {}'.format('Body', api_response.request.body))\n",
    "print('{:12s}: {}'.format('Cookies', api_response.cookies))\n",
    "print('{:12s}: {}'.format('Content', api_response.content))\n",
    "print('{:12s}: {}'.format('Text', api_response.text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "URL         : http://192.168.192.234:8888/text?params=params_value\n",
      "Path_URL    : /text?params=params_value\n",
      "StatusCode  : 200\n",
      "Method      : POST\n",
      "Encoding    : utf-8\n",
      "Headers     : {'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '16', 'Server': 'Werkzeug/0.14.1 Python/3.7.1', 'Date': 'Mon, 11 Jan 2021 04:17:11 GMT'}\n",
      "Body        : b'Text Test \\xe6\\xb5\\x8b\\xe8\\xaf\\x95'\n",
      "Cookies     : <RequestsCookieJar[]>\n",
      "Content     : b'Text Test \\xe6\\xb5\\x8b\\xe8\\xaf\\x95'\n",
      "Text        : Text Test 测试\n"
     ]
    }
   ],
   "source": [
    "# API 数据传输\n",
    "api_response = requests.post('{}/text'.format(URL),params={'params': 'params_value'}, data='Text Test 测试'.encode('utf-8'))\n",
    "print('{:12s}: {}'.format('URL', api_response.url))\n",
    "print('{:12s}: {}'.format('Path_URL', api_response.request.path_url))\n",
    "print('{:12s}: {}'.format('StatusCode', api_response.status_code))\n",
    "print('{:12s}: {}'.format('Method', api_response.request.method))\n",
    "print('{:12s}: {}'.format('Encoding', api_response.encoding))\n",
    "print('{:12s}: {}'.format('Headers', api_response.headers))\n",
    "print('{:12s}: {}'.format('Body', api_response.request.body))\n",
    "print('{:12s}: {}'.format('Cookies', api_response.cookies))\n",
    "print('{:12s}: {}'.format('Content', api_response.content))\n",
    "print('{:12s}: {}'.format('Text', api_response.text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "URL         : http://192.168.192.234:8888/json_data?params=params_value\n",
      "Path_URL    : /json_data?params=params_value\n",
      "StatusCode  : 200\n",
      "Method      : POST\n",
      "Encoding    : utf-8\n",
      "Headers     : {'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '62', 'Server': 'Werkzeug/0.14.1 Python/3.7.1', 'Date': 'Mon, 11 Jan 2021 04:17:17 GMT'}\n",
      "Body        : b'{\"ID\": 1, \"Time\": \"20210111 12:17:17\", \"Value\": \"JSON \\xe6\\xb5\\x8b\\xe8\\xaf\\x95\"}'\n",
      "Cookies     : <RequestsCookieJar[]>\n",
      "Content     : b'{\"ID\": 1, \"Time\": \"20210111 12:17:17\", \"Value\": \"JSON \\xe6\\xb5\\x8b\\xe8\\xaf\\x95\"}'\n",
      "Text        : {\"ID\": 1, \"Time\": \"20210111 12:17:17\", \"Value\": \"JSON 测试\"}\n",
      "\n",
      "\n",
      " {'ID': 1, 'Time': '20210111 12:17:17', 'Value': 'JSON 测试'}\n"
     ]
    }
   ],
   "source": [
    "import json \n",
    "import time \n",
    "\n",
    "data = {'ID': 1, 'Value': 'JSON 测试', 'Time': time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time()))}\n",
    "data = json.dumps(data, ensure_ascii=False, sort_keys=True)\n",
    "\n",
    "# API 数据传输\n",
    "api_response = requests.post('{}/json_data'.format(URL),params={'params': 'params_value'}, data= data.encode('utf-8'))\n",
    "print('{:12s}: {}'.format('URL', api_response.url))\n",
    "print('{:12s}: {}'.format('Path_URL', api_response.request.path_url))\n",
    "print('{:12s}: {}'.format('StatusCode', api_response.status_code))\n",
    "print('{:12s}: {}'.format('Method', api_response.request.method))\n",
    "print('{:12s}: {}'.format('Encoding', api_response.encoding))\n",
    "print('{:12s}: {}'.format('Headers', api_response.headers))\n",
    "print('{:12s}: {}'.format('Body', api_response.request.body))\n",
    "print('{:12s}: {}'.format('Cookies', api_response.cookies))\n",
    "print('{:12s}: {}'.format('Content', api_response.content))\n",
    "print('{:12s}: {}'.format('Text', api_response.text))\n",
    "\n",
    "print('\\n\\n', json.loads(api_response.text))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### CURL \n",
    "windows，-d参数的数据，需要使用双引号，json里的双引号使用反斜杠转义才可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "curl -H \"Content-Type:application/json\" -X POST http://192.168.0.106:8080/json_data  -d \"{\\\"ID\\\": 1, \\\"Time\\\": \\\"20210329 01:58:23\\\", \\\"Value\\\": \\\"JSON \\u6d4b\\u8bd5\\\"}\"   \n",
      "curl -H \"Content-Type:application/json\" -X POST http://192.168.0.106:8080/json_data  -d '{\"ID\": 1, \"Time\": \"20210329 01:58:23\", \"Value\": \"JSON 测试\"}'  \n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import time \n",
    "\n",
    "url = 'http://192.168.0.106:8080/json_data'\n",
    "\n",
    "data = {'ID': 1, 'Value': 'JSON 测试', 'Time': time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time()))}\n",
    "data = json.dumps(data, ensure_ascii=False, sort_keys=True)\n",
    "\n",
    "# CURL 命令\n",
    "string =\"\"\"curl -H \"Content-Type:application/json\" -X POST {}  -d \"{}\"   \"\"\".format(url, data.encode('unicode_escape').decode('utf8').replace('\"','\\\\\"'))\n",
    "print(string)\n",
    "\n",
    "# CURL 命令\n",
    "string =\"\"\"curl -H \"Content-Type:application/json\" -X POST {}  -d '{}'  \"\"\".format(url, data)\n",
    "print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"ID\": 1, \"Time\": \"20210329 01:58:23\", \"Value\": \"JSON \\u6d4b\\u8bd5\"}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n",
      "100   136  100    68  100    68   4250   4250 --:--:-- --:--:-- --:--:--  8500\n"
     ]
    }
   ],
   "source": [
    "! curl -H \"Content-Type:application/json\" -X POST http://192.168.0.106:8080/json_data  -d \"{\\\"ID\\\": 1, \\\"Time\\\": \\\"20210329 01:58:23\\\", \\\"Value\\\": \\\"JSON \\u6d4b\\u8bd5\\\"}\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start: 20210329 01:39:08 ........... Status: <OK> ........... Time: 20210329 01:58:47\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n",
      "100    85  100    85    0     0  85000      0 --:--:-- --:--:-- --:--:-- 85000\n"
     ]
    }
   ],
   "source": [
    "! curl -X POST  http://192.168.0.106:8080/health"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
